package com.tstudy.laoshibang.community;

import java.util.Calendar;
import java.util.List;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.HttpManager;
import com.tstudy.laoshibang.mode.Comment;
import com.tstudy.laoshibang.mode.User;
import com.tstudy.laoshibang.utils.CommonUtil;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * 评论页adapter
* @CopyRight tstudy.com.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date 2015年6月3日 上午11:24:26
 */
public class CommentListAdapter extends BaseAdapter{
	LayoutInflater mInflater;
	private List<Comment> mItems;
	
	public final int TYPE_EMPTY = 1;
	public final int TYPE_ITEM = 2;
	
	public CommentListAdapter() {
		 mInflater = LayoutInflater.from(BaseApplication.mContext);
	}
	
	@Override
	public int getCount() {
		return mItems == null ? 0:mItems.size();
	}
	
	@Override
	public int getViewTypeCount() {
		return 3;
	}
	@Override
	public int getItemViewType(int position) {
		return TextUtils.isEmpty(((Comment)getItem(position)).content)?TYPE_EMPTY:TYPE_ITEM;
	}
	
	public void setData(List<Comment> items) {
		setData(items,true);
	}
	public void setData(List<Comment> items,boolean isRefresh) {
		mItems = items;
		if (isRefresh) {
			notifyDataSetChanged();
		}
	}
	
	public List<Comment> getData() {
		return mItems;
	}
	

	@Override
	public Object getItem(int position) {
		return mItems == null ? null:mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		final EmptyViewHolder emptyViewHolder;
		switch (getItemViewType(position)) {
		case TYPE_ITEM:
			if(convertView ==  null){
				viewHolder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.comment_item, null);
				viewHolder.userAvatar = (ImageView)convertView.findViewById(R.id.comment_item_avatar);
				viewHolder.userName = (TextView)convertView.findViewById(R.id.comment_item_name);
				viewHolder.createTime = (TextView)convertView.findViewById(R.id.comment_item_time);
				viewHolder.content = (TextView) convertView.findViewById(R.id.comment_item_content);
				viewHolder.isOwnUser = (TextView) convertView.findViewById(R.id.comment_item_own);
				viewHolder.sp = convertView.findViewById(R.id.comment_item_sp);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}
			if(viewHolder != null){
				setConvertView(viewHolder,position,convertView);
			}
			break;
		case TYPE_EMPTY:
			if(convertView ==  null){
				emptyViewHolder = new EmptyViewHolder();
				convertView = mInflater.inflate(R.layout.message_empty_layout, null);
				convertView.setTag(emptyViewHolder);
			}else{
				emptyViewHolder = (EmptyViewHolder) convertView.getTag();
			}
			break;
		default:
			break;
		}
		return convertView;
	}
	
	public void setEmptyView(final ViewHolder viewHolder,final int position,View view){
		
	}
	
	public void setConvertView(final ViewHolder viewHolder,final int position,View view){
		final Comment comment = (Comment) getItem(position);
		if (comment.tcComId == 0) {
			view.setVisibility(View.INVISIBLE);
			return;
		}else{
			view.setVisibility(View.VISIBLE);
		}
		viewHolder.content.setText("  ");
		if (comment.toUser != null && comment.toUser.nick.length() > 0) {
			viewHolder.content.setText("回复 " + comment.toUser.nick + ": " + comment.content);
		}else{
			viewHolder.content.setText(comment.content);
		}
		if (!TextUtils.isEmpty(comment.createTime)) {
			Calendar calendar = Calendar.getInstance();
//			calendar.setTimeInMillis(Long.parseLong(comment.createTime));
			viewHolder.createTime.setText(CommonUtil.timelineDateStr(Long.parseLong(comment.createTime)));
		}
		if (comment.fromUser != null) {
			viewHolder.userName.setText(comment.fromUser.nick + "");
			viewHolder.isOwnUser.setVisibility(comment.fromUser.isSameUser==1?View.VISIBLE:View.GONE);
			HttpManager.getInstance().loadEditAvatar(viewHolder.userAvatar,comment.fromUser.imgIdxName,R.drawable.profile_avatar_default);
			view.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if (mActionCallBack != null) {
						mActionCallBack.onCommentClick(comment.fromUser,comment, position);
					}
				}
			});
			
			viewHolder.userAvatar.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (mActionCallBack != null) {
						mActionCallBack.onAvatarClick(comment.fromUser.userNo);
					}
				}
			});
			viewHolder.userName.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (mActionCallBack != null) {
						mActionCallBack.onAvatarClick(comment.fromUser.userNo);
					}
				}
			});
			if (BaseApplication.checkLogin() && comment.isDel==1){
				view.setOnLongClickListener(new OnLongClickListener() {
					@Override
					public boolean onLongClick(View v) {
						mActionCallBack.onDeleteClick(comment.tcComId, position);
						return false;
					}
				});
			}else{
				view.setOnLongClickListener(null);
			}
		}
		
	}
	
	static class ViewHolder{
		ImageView userAvatar;
		TextView userName;
		TextView createTime;
		TextView content;
		TextView isOwnUser;
		View sp;
	}
	
	static class EmptyViewHolder{
		ImageView emptyIcon;
		TextView emptyDesc;
	}
	
	public interface AdapterCallBack{
		public void onAvatarClick(String userNo);
		public void onCommentClick(User toUser,Comment comment,int position);
		public void onDeleteClick(int commentId,int position);
	}
	AdapterCallBack mActionCallBack;
	public void setAdatperCallBack(AdapterCallBack actionCallBack){
		mActionCallBack = actionCallBack;
	}
}
